package com.sunyur.tailor.channel.service.inner.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.inner.util.SunyurResultUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelListResultUtil;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.product.model.BaseProductInfoModel;
import com.sunyur.tailor.channel.service.inner.product.model.CategoryModel;
import com.sunyur.tailor.channel.service.inner.product.model.DescProductInfoModel;
import com.sunyur.tailor.channel.service.inner.product.param.CategoryParam;
import com.sunyur.tailor.channel.service.inner.product.param.ProductInfoParam;
import com.sunyur.tailor.channel.service.inner.product.param.ReplaceItemMaterialParam;
import com.sunyur.tailor.channel.service.inner.product.service.SunyurProductService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author XuHui
 * @Description 商品相关接口实现
 * @Date 16:26 2021/8/15
 **/
@Service("sunyurProductService")
public class SunyurProductServiceImpl implements SunyurProductService {

    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    /**
     * 37-批量获取商品基本信息 链接
     */
    @Value("${sunyur.api.listBaseProductInfo}")
    private String baseProductInfo;
    /**
     * 38-批量获取商品详情描述信息 链接
     */
    @Value("${sunyur.api.listDescProductInfo}")
    private String descProductInfo;

    /**
     * 38-批量获取商品详情描述信息 链接
     */
    @Value("${sunyur.api.batchReplaceItemMaterial}")
    private String batchReplaceItemMaterial;

    /**
     * 32-获取商城分类通过【内部商城分类编码】
     */
    @Value("${sunyur.api.listCategoryBySystemCode}")
    private String listCategoryBySystemCode;


    @Override
    public ChannelResult<List<BaseProductInfoModel>> getBaseProductInfo(ProductInfoParam productInfoParam) {
        if (ObjectUtils.isEmpty(productInfoParam) || CollectionUtils.isEmpty(productInfoParam.getMallSkuIdList())) {
            return ChannelListResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
        }
        // 调用主线接口
        SunyurResult<List<BaseProductInfoModel>> sunyurResult = sunyurClient.execPost(baseProductInfo, JSON.toJSONString(productInfoParam), new TypeReference<SunyurResult<List<BaseProductInfoModel>>>() {
        });
        if (SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.success(sunyurResult.getContent());
        }
        return ChannelResultUtil.error(sunyurResult.getCode(), sunyurResult.getErrorMessage());
    }

    @Override
    public ChannelResult<List<DescProductInfoModel>> getDescProductInfo(ProductInfoParam productInfoParam) {
        if (ObjectUtils.isEmpty(productInfoParam) || CollectionUtils.isEmpty(productInfoParam.getMallSkuIdList())) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
        }
        // 调用主线接口
        SunyurResult<List<DescProductInfoModel>> sunyurResult = sunyurClient.execPost(descProductInfo, JSON.toJSONString(productInfoParam), new TypeReference<SunyurResult<List<DescProductInfoModel>>>() {
        });
        if (SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.success(sunyurResult.getContent());
        }
        return ChannelResultUtil.error(sunyurResult.getCode(), sunyurResult.getErrorMessage());
    }

    @Override
    public ChannelResult<Boolean> batchReplaceItemMaterial(List<ReplaceItemMaterialParam> replaceIMaterialParamList) {
        if (CollectionUtils.isEmpty(replaceIMaterialParamList)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
        }
        // 调用主线接口
        SunyurResult<Boolean> sunyurResult = sunyurClient.execPost(batchReplaceItemMaterial, JSON.toJSONString(replaceIMaterialParamList), new TypeReference<SunyurResult<Boolean>>() {
        });
        if (SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.success(sunyurResult.getContent());
        }
        return ChannelResultUtil.error(sunyurResult.getCode(), sunyurResult.getErrorMessage());
    }

    @Override
    public ChannelResult<List<CategoryModel>> getCategoryBySystemCode(CategoryParam categoryParam) {
        if (ObjectUtils.isEmpty(categoryParam) || CollectionUtils.isEmpty(categoryParam.getSystemCategoryCodeList())) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
        }
        // 调用主线接口
        SunyurResult<List<CategoryModel>> sunyurResult = sunyurClient.execPost(listCategoryBySystemCode, JSON.toJSONString(categoryParam), new TypeReference<SunyurResult<List<CategoryModel>>>() {
        });
        if (SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.success(sunyurResult.getContent());
        }
        return ChannelResultUtil.error(sunyurResult.getCode(), sunyurResult.getErrorMessage());
    }
}
